package ru.neoflex.chugunov.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import ru.neoflex.chugunov.RateWordsHandler;
import ru.neoflex.chugunov.Word;

public class Card {
	public static void createAndShowUI(Word word) {
		JFrame frame = new JFrame("Card");
		frame.getContentPane().add(new MainGUI(word).getMainPanel());
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}

class MainGUI {
	private static final Dimension MAIN_PANEL_SIZE = new Dimension(270, 225);
	private JPanel mainPanel = new JPanel();
	private JDialog modalDialog;
	private Word word;

	public MainGUI(Word word) {
		this.word = word;
		JButton showAnswer = new JButton("Show answer");
		JLabel wordLabel = new JLabel(word.getWord());
		showAnswer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAnswerPerformed(e);
			}
		});
		mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
		mainPanel.add(wordLabel);
		mainPanel.add(showAnswer);
	}

	private void showAnswerPerformed(ActionEvent e) {
		if (modalDialog == null) {
			Window topWindow = SwingUtilities.getWindowAncestor(mainPanel);
			modalDialog = new JDialog(topWindow, "ANSWER",
					ModalityType.APPLICATION_MODAL);
			modalDialog.getContentPane().add(
					new DialogPanel(word).getDialogPanel());
			modalDialog.pack();
			modalDialog.setLocationRelativeTo(topWindow);
			modalDialog.setVisible(true);
		} else {
			modalDialog.setVisible(true);
		}
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}
}

class DialogPanel {
	private static final Dimension DIALOG_SIZE = new Dimension(480, 120);
	private JPanel dialogPanel = new JPanel();
	private JPanel leftPanel = new JPanel();
	private JPanel rightPanel = new JPanel();
	private JButton zeroBtn = new JButton("0");
	private JButton oneBtn = new JButton("1");
	private JButton twoBtn = new JButton("2");
	private JButton threeBtn = new JButton("3");
	private JButton fourBtn = new JButton("4");
	private JButton fiveBtn = new JButton("5");

	public DialogPanel(Word word) {
		dialogPanel.add(leftPanel, BorderLayout.WEST);
		dialogPanel.add(rightPanel, BorderLayout.EAST);
		leftPanel.add(new JLabel(word.getTranslation()));

		rightPanel.add(new JLabel("Appreciate the ease answer",
				SwingConstants.CENTER), new Integer(2));
		rightPanel.add(zeroBtn, new Integer(1));
		rightPanel.add(oneBtn, new Integer(1));
		rightPanel.add(twoBtn, new Integer(1));
		rightPanel.add(threeBtn, new Integer(1));
		rightPanel.add(fourBtn, new Integer(1));
		rightPanel.add(fiveBtn, new Integer(1));
		zeroBtn.addActionListener(new ActionListenerBtn(word));
		oneBtn.addActionListener(new ActionListenerBtn(word));
		twoBtn.addActionListener(new ActionListenerBtn(word));
		threeBtn.addActionListener(new ActionListenerBtn(word));
		fourBtn.addActionListener(new ActionListenerBtn(word));
		fiveBtn.addActionListener(new ActionListenerBtn(word));
		dialogPanel.setPreferredSize(DIALOG_SIZE);
	}

	public JPanel getDialogPanel() {
		return dialogPanel;
	}
}